package org.ms.mtx.core.configuration;

import lombok.Getter;
import org.ms.mtx.core.aspect.AbstractAspect;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * The Transactional Aspect Configuration
 *
 * @author lry
 */
@Getter
@Configuration
@EnableConfigurationProperties(TransactionProperties.class)
public class TransactionalConfiguration extends AbstractAspect implements BeanPostProcessor {

    @Resource
    private TransactionProperties transactionProperties;
    @Resource
    private ApplicationContext applicationContext;

    private Map<String, String> beanNameMap = new HashMap<>();

    // ================== post process before and after initialization

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        beanNameMap.put(bean.toString(), beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    // ================== confirm and cancel acquisition


    @Override
    protected Object getObjectByClass(Class<?> clazz) {
        return applicationContext.getBean(clazz);
    }

}
